
//#############################################################################
//
//! \file  User_Component/Oled/Oled.c
//!
//! \brief Oled Software dirve
//!
//  Author: Walter Mitty
//  Group:          C2000
//  Target Device:  TMS320F28027
//#############################################################################
 // Date: 2017.11.05 $
//#############################################################################

// **************************************************************************
// the includes
#include "User_Component/OLED/Font.h"
#include "User_Component/OLED/OLED.h"

// **************************************************************************
// the defines

// **************************************************************************
// the globals

// **************************************************************************
// the functions

// the function prototypes
//! \brief     OLED initail
//! \param[in] None
//! \param[out] None
void OLED_initial(void)
{
    // 1. set which_Mode[which_Phase-1]
    //void GPIO_setMode(GPIO_Handle gpioHandle,const GPIO_Number_e gpioNumber,const GPIO_Mode_e which_Mode[which_Phase-1]);
    GPIO_setMode(OLED_obj, OLED_DA, GPIO_16_Mode_GeneralPurpose);
    GPIO_setMode(OLED_obj, OLED_DC, GPIO_17_Mode_GeneralPurpose);
    GPIO_setMode(OLED_obj, OLED_CLK, GPIO_18_Mode_GeneralPurpose);
    GPIO_setMode(OLED_obj, OLED_RST, GPIO_19_Mode_GeneralPurpose);
    // 2. set pullup
    //void GPIO_setPullUp(GPIO_Handle gpioHandle,const GPIO_Number_e gpioNumber,const GPIO_PullUp_e pullUp);
    GPIO_setPullUp(OLED_obj, OLED_CLK, GPIO_PullUp_Disable);
    GPIO_setPullUp(OLED_obj, OLED_DA, GPIO_PullUp_Disable);
    GPIO_setPullUp(OLED_obj, OLED_RST, GPIO_PullUp_Disable);
    GPIO_setPullUp(OLED_obj, OLED_DC, GPIO_PullUp_Disable);
    //3. set direction
    //void GPIO_setDirection(GPIO_Handle gpioHandle,const GPIO_Number_e gpioNumber,const GPIO_Direction_e direction);
    GPIO_setDirection(OLED_obj, OLED_CLK, GPIO_Direction_Output);
    GPIO_setDirection(OLED_obj, OLED_DA, GPIO_Direction_Output);
    GPIO_setDirection(OLED_obj, OLED_RST, GPIO_Direction_Output);
    GPIO_setDirection(OLED_obj, OLED_DC, GPIO_Direction_Output);

}
//
//! \brief     OLED configure
//! \param[in] None
//! \param[out] None
void OLED_config(void)
{
    //configure OLED
    OLED_setHigh(OLED_CLK);
    OLED_setLow(OLED_RST);
    wOLED_delay(200);
    OLED_setHigh(OLED_RST);    //���ϵ絽���濪ʼ��ʼ��Ҫ���㹻��ʱ�䣬���ȴ�RC��λ���
    wOLED_WriteCommand(0xae);    //--turn off oled panel
    wOLED_WriteCommand(0x00);    //---set low column address
    wOLED_WriteCommand(0x10);    //---set high column address
    wOLED_WriteCommand(0x40); //--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
    wOLED_WriteCommand(0x81);    //--set contrast control register
    wOLED_WriteCommand(0xcf); // Set SEG Output Current Brightness
    wOLED_WriteCommand(0xa1); //--Set SEG/Column Mapping     0xa0���ҷ��� 0xa1����
    wOLED_WriteCommand(0xc8); //Set COM/Row Scan Direction   0xc0���·��� 0xc8����
    wOLED_WriteCommand(0xa6); //--set normal display
    wOLED_WriteCommand(0xa8); //--set multiplex ratio(1 to 64)
    wOLED_WriteCommand(0x3f); //--1/64 duty
    wOLED_WriteCommand(0xd3); //-set display offset   Shift Mapping RAM Counter (0x00~0x3F)
    wOLED_WriteCommand(0x00); //-not offset
    wOLED_WriteCommand(0xd5); //--set display clock divide ratio/oscillator frequency
    wOLED_WriteCommand(0x80); //--set divide ratio, Set Clock as 100 Frames/Sec
    wOLED_WriteCommand(0xd9); //--set pre-charge period
    wOLED_WriteCommand(0xf1); //Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
    wOLED_WriteCommand(0xda); //--set com pins hardware configuration
    wOLED_WriteCommand(0x12);
    wOLED_WriteCommand(0xdb); //--set vcomh
    wOLED_WriteCommand(0x40); //Set VCOM Deselect Level
    wOLED_WriteCommand(0x20); //-Set Page Addressing Mode (0x00/0x01/0x02)
    wOLED_WriteCommand(0x02); //
    wOLED_WriteCommand(0x8d); //--set Charge Pump enable/disable
    wOLED_WriteCommand(0x14); //--set(0x10) disable
    wOLED_WriteCommand(0xa4); // Disable Entire Display On (0xa4/0xa5)
    wOLED_WriteCommand(0xa6); // Disable Inverse Display On (0xa6/a7)
    wOLED_WriteCommand(0xaf); //--turn on oled panel
    wOLED_FullShow(0x00);    //��ʼ����
}
////////////////////////////////////////////////////////////////////////////////////////////
//! \brief     OLED delay
//! \param[in] None
//! \param[out] None
void wOLED_delay(uint32_t mms)
{
    while (mms--)
        ;
}

void wOLED_SetPage(uint16_t Page)
{
    wOLED_WriteCommand(0xb0 + Page -1);                  //����ҳ��ַ
}

void wOLED_SetRank(uint16_t Rank)
{
    wOLED_WriteCommand(0x01 + (0x0F & (Rank--)));         //������ʼ�е�ַ�ĵ���λ
    wOLED_WriteCommand(0x10 + ((0xF0 & Rank) >> 4));      //������ʼ�е�ַ�ĸ���λ
}




//! \brief     OLED write command
//! \param[in] None
//! \param[out] None
void wOLED_WriteCommand(uint16_t command)
{
    uint16_t i;
    OLED_setLow(OLED_DC);  //command
    for (i = 0; i < 8; i++) //����һ����λ����
    {
        if ((command << i) & 0x80)
            OLED_setHigh(OLED_DA);
        else
            OLED_setLow(OLED_DA);

        OLED_setLow(OLED_CLK);
        OLED_setHigh(OLED_CLK);
    }
}


//! \brief     OLED write data
//! \param[in] None
//! \param[out] None
void wOLED_WriteData(uint16_t data)
{
    uint16_t i;
    OLED_setHigh(OLED_DC);  //data
    for (i = 0; i < 8; i++) //����һ����λ����
    {
        if ((data << i) & 0x80)
            OLED_setHigh(OLED_DA);
        else
            OLED_setLow(OLED_DA);
        OLED_setLow(OLED_CLK);
        OLED_setHigh(OLED_CLK);
    }
}




//! \brief     OLED full show  ONE char
//! \param[in] None
//! \param[out] None
void wOLED_FullShow(uint16_t data)
{
    uint16_t y, x;
    for (y = 1; y <= 8; y++)
    {
        wOLED_SetRank(1);              //�����е�ַ
        wOLED_SetPage(y);              //����ҳ��ַ
        for (x = 0; x < 128; x++)
            wOLED_WriteData(data);
    }
}

 void wShowOLED16X8(uint16_t Page, uint16_t Rank, uint16_t Over)
{
    uint16_t i, j;
    for (i = 0; i < 2; i++)
    {
        wOLED_SetRank(Rank);                  //�����е�ַ
        wOLED_SetPage(Page + i);              //����ҳ��ַ
        for (j = 0; j < 8; j++)
            wOLED_WriteData(my16x8[j + 8 * (i + Over*2)]);
    }
}

 void wShowOLED16X16(uint16_t Page, uint16_t Rank, uint16_t Over)
{
    uint16_t i, j;
    for (i = 0; i < 2; i++)
    {
        wOLED_SetRank(Rank);
        wOLED_SetPage(Page + i);
        for (j = 0; j < 16; j++)
            wOLED_WriteData(my16x16[j + 16 * (i + Over*2)]);
    }
}

 ////////////////////////////////////////////////////////////////////////////////////////////
void wShowOLED4Corners()
{
    /*128*64 => 128*8*/

    //(1,1)
    wOLED_SetRank(1);
    wOLED_SetPage(1);
    wOLED_WriteData(0x01);             //2#1000_0000#

    //(128,1)
    wOLED_SetRank(128);
    wOLED_SetPage(1);
    wOLED_WriteData(0x01);              //2#1000_0000#

    //(1,64)
    wOLED_SetRank(1);
    wOLED_SetPage(8);
    wOLED_WriteData(0x80);             //2#1000_0000#

    //(128,64)
    wOLED_SetRank(128);
    wOLED_SetPage(8);
    wOLED_WriteData(0x80);             //2#1000_0000#


    wShowOLED16X16(1,50,0);
    wShowOLED16X16(1,50+16,1);

}



void wShowProgressBar()
{

    static int progress = 0;
    if (progress < 100)
        progress += 1;
    else
        progress = 0;


    int hundred = progress / 100;
    int ten = progress / 10;
    int single = progress % 10;

    if (hundred)
        wShowOLED16X8(1, 50, hundred);
    else
        wShowOLED16X8(1, 50, 30);

    if (ten == 0)
        wShowOLED16X8(1, 50 + 8, 30);
    else if (ten < 10)
        wShowOLED16X8(1, 50 + 8, ten);
    else
        wShowOLED16X8(1, 50 + 8, 0);

    wShowOLED16X8(1, 50 + 16, single);

    wShowOLED16X8(1, 50 + 24, 14);


    int i = 0;
    wOLED_SetRank(1);
    wOLED_SetPage(5);
    for (i = 1; i <= 128; i++)
        wOLED_WriteData(0x00);           //2#0000_0000#

    wOLED_SetRank(14);
    wOLED_SetPage(5);
    for (i = 0; i < progress; i++)
        wOLED_WriteData(0xFF);            //2#1111_1111#


    if (progress < 100)
        wOLED_delay(1E5);
    else
    {
        wOLED_delay(3E6);
        wOLED_FullShow(0x00);
    }


}




// end of file
